<?php

class M_mkaryawan extends CI_Model{
	
	function M_mkaryawan(){
		parent::__construct();
	}
	
	function mkaryawan_list(){
		$query = "SELECT NIK, GRADE, KODEUNIT, KODEJAB, TGLREVISI, NAMAKAR
		FROM karyawan";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($mkaryawan_nik, $mkaryawan_grade, $mkaryawan_kodeunit, $mkaryawan_kodejab, 
				$mkaryawan_tglrevisi, $mkaryawan_namakar){
		$sql = "SELECT NIK FROM karyawan WHERE NIK='".$mkaryawan_nik."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"NIK"=>$mkaryawan_nik,
					"GRADE"=>$mkaryawan_grade,
					"KODEUNIT"=>$mkaryawan_kodeunit,
					"KODEJAB"=>$mkaryawan_kodejab,
					"TGLREVISI"=>$mkaryawan_tglrevisi,
					"NAMAKAR"=>$mkaryawan_namakar
			);
			$this->db->insert('karyawan', $data);
			if($this->db->affected_rows())
				return 1;
			else
				return 0;
		}
	}
	
	function update($mkaryawan_niklama, $mkaryawan_nik, $mkaryawan_grade, $mkaryawan_kodeunit, $mkaryawan_kodejab, 
				$mkaryawan_tglrevisi, $mkaryawan_namakar){
		$data = array(
				"GRADE"=>$mkaryawan_grade,
				"KODEUNIT"=>$mkaryawan_kodeunit,
				"KODEJAB"=>$mkaryawan_kodejab,
				"TGLREVISI"=>$mkaryawan_tglrevisi,
				"NAMAKAR"=>$mkaryawan_namakar
		);
		if($mkaryawan_nik != $mkaryawan_niklama){
			$data["NIK"] = $mkaryawan_nik;
		}
		
		$this->db->where('NIK', $mkaryawan_niklama);
		$this->db->update('karyawan', $data);
		if($this->db->affected_rows())
			return 1;
		else
			return 0;
	}
	
}

?>